﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Data;
using System.Windows.Forms;

namespace TMS
{
    class Partner
    {
        private int ID;
        private string Name;
        private string Address;
        private string Postal_Code;
        private string City;
        private string Phone_Num;
        private string Tax_Num;
        private string Country;
        private string Email;
        private string Contact;

        public Partner()
        { 
        
        }

        //sluzi za brisenje
        public Partner(int ID)
        {
            this.ID = ID;
        }

        public Partner(int ID, string Name, string Address, string Postal_Code, string City, string Phone_Num, string Tax_Num, string Country, string Email, string Contact) 
        { 
            this.ID = ID;
            this.Name = Name;
            this.Address = Address;
            this.Postal_Code = Postal_Code;
            this.City = City;
            this.Phone_Num = Phone_Num;
            this.Tax_Num = Tax_Num;
            this.Country = Country;
            this.Email = Email;
            this.Contact = Contact;
        }

        public void InsertPartnerInDB()
        {

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();


            string sqlInsertPartner = "INSERT INTO Partners VALUES (DEFAULT,'" +
                                        this.Name + "','" +
                                        this.Address + "','" +
                                        this.Postal_Code + "','" +
                                        this.City + "','" +
                                        this.Phone_Num + "','" +
                                        this.Tax_Num + "','" +
                                        this.Country + "','" +
                                        this.Email + "','" +
                                        this.Contact + "')";
            MySqlCommand cmd = new MySqlCommand(sqlInsertPartner, mysqlCon);
            cmd.ExecuteNonQuery();

            mysqlCon.Close();
        }

        public void UpdatePartnerInDB()
        {

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();

            string sqlUpdatePartner = "UPDATE Partners " +
                                      "SET name ='" + this.Name + "'," +
                                      "address ='" + this.Address + "', " +
                                      "postal_code ='" + this.Postal_Code + "', " +
                                      "city ='" + this.City + "', " +
                                      "phone ='" + this.Phone_Num + "', " +
                                      "tax_number = '" + this.Tax_Num + "', " +
                                      "country = '" + this.Country + "', " +
                                      "email = '" + this.Email + "', " +
                                      "contact = '" + this.Contact + "' " +
                                      " WHERE ID = " + this.ID;
            

            MySqlCommand cmd = new MySqlCommand(sqlUpdatePartner, mysqlCon);
            
            cmd.ExecuteNonQuery();

            mysqlCon.Close();

        }

        public void DeletePartnerFromDB()
        {

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();

            string sqlDeletePartner = "DELETE FROM Partners " +
                                      "WHERE ID = " + this.ID;

            MySqlCommand cmd = new MySqlCommand(sqlDeletePartner, mysqlCon);
            cmd.ExecuteNonQuery();

            mysqlCon.Close();
        }

        public bool CheckExistingPartnerInDB() {

            return false;


            //string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            //MySqlConnection mysqlCon = new MySqlConnection(connString);

            //mysqlCon.Open();

            //string sqlCheckExistingPartner = "";

            //MySqlCommand cmd = new MySqlCommand(sqlCheckExistingPartner, mysqlCon);

            //object result = cmd.ExecuteScalar();
            //mysqlCon.Close();

            //if (result != DBNull.Value && result != null)
            //{
            //    //record exists
            //    return true;
            //}
            //else
            //{
            //    //does not exist
            //    return false;
            //}
        }

        public DataTable GetAllPartners() {

            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            DataSet table = new DataSet();

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();

            string sqlSelectPartners = "SELECT * FROM partners";

            MyDA.SelectCommand = new MySqlCommand(sqlSelectPartners, mysqlCon);
            MyDA.Fill(table);

            mysqlCon.Close();

            return table.Tables[0];
        }

        public DataTable GetPartner(int ID) {

            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            DataSet table = new DataSet();

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();

            string sqlSelectPartners = "SELECT name FROM partners WHERE ID="+ID;

            MyDA.SelectCommand = new MySqlCommand(sqlSelectPartners, mysqlCon);
            MyDA.Fill(table);

            mysqlCon.Close();

            return table.Tables[0];
        }

        public string GetPartnerAddressByName(string Name)
        {

            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            DataSet table = new DataSet();

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);

            mysqlCon.Open();

            string sqlSelectPartners = "SELECT address FROM partners WHERE name='" + Name+"'";
           // MessageBox.Show(sqlSelectPartners);
            MyDA.SelectCommand = new MySqlCommand(sqlSelectPartners, mysqlCon);
            MyDA.Fill(table);

            mysqlCon.Close();

            return table.Tables[0].Rows[0].ItemArray[0].ToString();
        }
    }
}
